package com.jy.adscm.operation.cg.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.QueryCgBom;
import com.jy.adscm.operation.cg.service.CgBomService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * bom 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@RestController
@RequestMapping("adscm/operation/cg/cgBom")
public class CgBomController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    CgBomService cgBomService;

    /**
     * 增加bom信息
     * @param cgBom
     * @param request
     * @return
     */
    @AdscmLogger("增加bom信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody CgBom cgBom, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        cgBom.setCreator(token.getUser().getId().toString());
        cgBom.setCreateTime(new Date());
        cgBom.setEnable(1);
        boolean insert = cgBomService.insert(cgBom);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    /**
     * 修改bom信息
     * @param cgBom
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @AdscmLogger("修改bom信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody CgBom cgBom, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        cgBom.setCreator(token.getUser().getId().toString());
        cgBom.setCreateTime(new Date());
        boolean result = cgBomService.updateById(cgBom);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    @RequestMapping("delete/{id}")
    public  SimpleCoreResponse  delete(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean result = cgBomService.deleteCgBom(id);
        return  new SimpleCoreResponse("200","删除成功",result);
    }

    /**
     * 根据图纸id查询bom信息
     * @param drawId
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectByDrawId/{drawId}")
    public  SimpleCoreResponse  selectByDrawId(@PathVariable Integer drawId , HttpServletRequest request) throws  AdscmSystemException{

        List<CgBom> cgBoms = cgBomService.selectByDrawId(drawId);
        return  new SimpleCoreResponse("200","查询成功",cgBoms);
    }
    /**
     * 根据图纸parentId查询图纸信息
     * @param parentId
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectDrawByParentId/{parentId}")
    public  SimpleCoreResponse  selectDrawByParentId(@PathVariable Integer parentId , HttpServletRequest request) throws  AdscmSystemException{
         List<JyDrawingList>   jyDrawingLists= cgBomService.selectDrawByParentId(parentId);
        return  new SimpleCoreResponse("200","查询成功",jyDrawingLists);
    }
    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll( HttpServletRequest request) throws  AdscmSystemException{
        List<CgBom> cgBoms = cgBomService.selectList(null);
        return  new SimpleCoreResponse("200","查询成功",cgBoms);
    }

    @RequestMapping("selectByMid/{id}")
    public  SimpleCoreResponse  selectByMid(@PathVariable Integer id,  HttpServletRequest request) throws  AdscmSystemException{
        List<CgBom> cgBoms = cgBomService.selectByMid(id);
        return  new SimpleCoreResponse("200","查询成功",cgBoms);
    }





}

